﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Word = Microsoft.Office.Interop.Word;

namespace FindModel
{
    public class WordHelper
    {
        private object oEndOfDoc = "\\endofdoc";//WORD中预定义的书签

        /// <summary>
        /// 生成word
        /// </summary>
        /// <param name="dt"></param>
        public void CreateWord(IList<TableInfo> list)
        {
            if (!list.Any())
            {
                return;
            }
            Word.ApplicationClass app = null;
            Word.Document doc = null;
            try
            {

                app = new Word.ApplicationClass();
                app.Visible = true;
                doc = app.Documents.Add();
                Word.Range wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;//获取当前文档的末尾位置。 
                int i = 2;
                wrdRng.InsertAfter("\r\n ");//插入一行
                wrdRng.InsertAfter($"1.1 表清单");//插入一行
                wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;//获取当前文档的末尾位置。 
                Word.Table table = doc.Tables.Add(wrdRng, list.Count + 1, 2);
                doc.Tables[1].Cell(1, 1).Range.Text = "表名";
                doc.Tables[1].Cell(1, 2).Range.Text = "说明";
                foreach (var item in list)
                {
                    doc.Tables[1].Cell(i, 1).Range.Text = item.Code;
                    doc.Tables[1].Cell(i, 2).Range.Text = item.Name;
                    object missing = Type.Missing;
                    wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;//获取当前文档的末尾位置。 
                    wrdRng.InsertAfter("\r\n");//插入一行
                    wrdRng.InsertAfter($"1.{i} {item.Name}:{item.Code}");//插入一行
                    wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                    wrdRng.InsertParagraphAfter();//插入一个段落
                    Word.Table newTable = doc.Tables.Add(wrdRng, item.Columns.Count + 1, 6);
                    doc.Tables[i].Cell(1, 1).Range.Text = "字段名称";
                    doc.Tables[i].Cell(1, 2).Range.Text = "中文名称";
                    doc.Tables[i].Cell(1, 3).Range.Text = "数据库类型";
                    doc.Tables[i].Cell(1, 4).Range.Text = "可空";
                    doc.Tables[i].Cell(1, 5).Range.Text = "主键";
                    doc.Tables[i].Cell(1, 6).Range.Text = "备注";
                    int j = 2;
                    foreach (var colum in item.Columns)
                    {

                        doc.Tables[i].Cell(j, 1).Range.Text = colum.Code;
                        doc.Tables[i].Cell(j, 2).Range.Text = colum.Name;
                        doc.Tables[i].Cell(j, 3).Range.Text = colum.DataType;
                        doc.Tables[i].Cell(j, 4).Range.Text = colum.Mandatory ? "" : "是";
                        doc.Tables[i].Cell(j, 5).Range.Text = colum.IsPrimaryKey ? "是" : "";
                        doc.Tables[i].Cell(j, 6).Range.Text = colum.Comment;
                        j++;
                    }
                    newTable.Select();
                    newTable.Rows.Alignment = Word.WdRowAlignment.wdAlignRowCenter;
                    newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    app.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                    i++;
                }
                table.Select();
                table.Rows.Alignment = Word.WdRowAlignment.wdAlignRowCenter;
                table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                object address = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "数据库表.docx");
                doc.SaveAs(ref address);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
    }
}
